import { Injectable } from '@nestjs/common';
// import { ConfigService } from '@nestjs/config';
import { Collection } from './collection.entity';
import { DataSource, Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { PagingResponse } from 'src/constants/common.constants';
import { AppCollectionListDto } from './dto/list.dto';
import { AddCollectionDto } from './dto/add.dto';
// import { PassThrough } from 'stream';
@Injectable()
export class AppCollectionService {
  private access_token: string;
  private expires_in: number;
  private timestamp: number;

  constructor(
    @InjectRepository(Collection)
    private readonly appCollectionRepository: Repository<Collection>,
    private readonly dataSource: DataSource,
  ) {}

  async findAll(
    uid: string,
    params: AppCollectionListDto,
  ): Promise<PagingResponse<Collection>> {
    console.log('params', params);
    // console.log(
    //   this.configService.get('DATABASE_HOST'),
    //   this.configService.get('database.host'),
    //   await this.cacheManager.get('key'),
    //   await this.userRepository.findAndCount({
    //     where: {
    //       ...params.params,
    //     },
    //   }),
    // );
    const [items, total] = await this.appCollectionRepository.findAndCount({
      where: {
        // ...params.params,
        // type: 1,
        userId: uid,
      },
    });
    const { pageSize, pageNum } = params;
    return {
      records: items,
      total,
      pageSize,
      current: pageNum,
      pages: Math.ceil(total / pageSize),
    };
    // const [items] = await this.AppCollectionRepository.findAndCount({});
    // return items;
    // return await this.userRepository
    //   .createQueryBuilder()
    //   .where({ deleted: 0 })
    //   .select(['user_name', 'email'])
    //   .getMany();
    //   .find();
  }

  async findOne(id: number) {
    return await this.appCollectionRepository.findOne({
      where: {
        id,
      },
    });
  }

  async findOneByGatherId(gatherId: number, userId: string) {
    return await this.appCollectionRepository.findOne({
      where: {
        gatherId,
        userId,
      },
    });
  }

  async create(
    userId: string,
    collection: AddCollectionDto,
  ): Promise<Collection> {
    const data = this.appCollectionRepository.create({
      userId,
      ...collection,
    });
    return this.appCollectionRepository.save(data);
  }

  async remove(gatherId: number, userId: string): Promise<void> {
    await this.appCollectionRepository.delete({
      gatherId,
      userId,
    });
  }
}
